package com.sunday.aomi.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.aomi.R;
import com.sunday.aomi.base.BaseApp;
import com.sunday.aomi.ui.common.MainActivity;
import com.sunday.member.SessionToken;
import com.sunday.member.entity.Member;
import com.sunday.member.http.MemberClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/22.
 */

public class ShopLoginActivity extends BaseActivity {

    @Bind(R.id.login_type)
    TextView loginType;
    @Bind(R.id.btn_cancel)
    ImageView btnCancel;
    @Bind(R.id.login_logo)
    ImageView loginLogo;
    @Bind(R.id.edit_account)
    ClearEditText editAccount;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.login_layout)
    LinearLayout loginLayout;
    @Bind(R.id.forget_pwd)
    TextView forgetPwd;
    @Bind(R.id.btn_login_shop)
    TextView btnLoginShop;
    @Bind(R.id.or)
    TextView or;
    @Bind(R.id.btn_login_vip)
    TextView btnLoginVip;
    @Bind(R.id.login_remind)
    TextView loginRemind;

    private boolean isVip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_login);
        ButterKnife.bind(this);
        isVip = getIntent().getBooleanExtra("isVip", false);
        initView();
    }

    private void initView(){
        if (isVip){
            btnCancel.setVisibility(View.VISIBLE);
            loginType.setText("VIP登录");
            or.setVisibility(View.GONE);
            btnLoginVip.setVisibility(View.GONE);
            loginRemind.setVisibility(View.GONE);
        }
    }


    private String userName, userPwd;

    @OnClick({R.id.btn_login_shop, R.id.btn_login_vip,R.id.btn_cancel,R.id.forget_pwd})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login_shop:
                userName = editAccount.getText().toString().trim();
                userPwd = editPwd.getText().toString().trim();
                if (TextUtils.isEmpty(userName)) {
                    ToastUtils.showToast(mContext, "请填写正确账号");
                    return;
                }
                if (TextUtils.isEmpty(userPwd)) {
                    ToastUtils.showToast(mContext, "请填写正确密码");
                    return;
                }
                submit();
                break;
            case R.id.btn_login_vip:
                intent = new Intent(mContext, ShopLoginActivity.class);
                intent.putExtra("isVip", true);
                startActivity(intent);
                break;
            case R.id.btn_cancel:
                onBackPressed();
                break;
            case R.id.forget_pwd:
                intent = new Intent(mContext, ForgetPwdActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void submit() {
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = MemberClient.getApiAdapter().login(userName, userPwd, 2);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    if (resultDO.getResult() == null) {
                        return;
                    }
                    SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN,true);
                    BaseApp.getInstance().setMember(resultDO.getResult());
                    SessionToken.accountId = String.valueOf(resultDO.getResult().getId());
                    SessionToken.token = resultDO.getResult().getToken();
                    intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
}
